<?php



class Rake extends CI_Controller{

	

	var $db;



	public function database($version = false){

		

		// get current version

		$this->db = $this->load->database('', true);

		$this->load->library('Databasemigrations');

		

		$q = $this->db->get_where('meta', array('name'=>'database_version') );

		$r = $q->row_array();

		$current_version = $r['value'];

		//if $version == false go all way to the end

		if($version === false){

			$i = $current_version + 1;

			$max = 100;

			while( $max >= $i && $this->database_migrations($i, 'up') ){

				$i++;

			}

		// if $version is greater than current, roll up

		}elseif($version > $current_version){

			$i = $current_version + 1;

			$max = $version;

			while( $max >= $i && $this->database_migrations($i, 'up') ){

				echo "Upgrading database to version ".$i.PHP_EOL;

				$i++;

			}			

		// if $version is less, roll back		

		}elseif($version < $current_version){

			$i = $current_version;

			$max = $version;

			while(  $max <= $i && $this->database_migrations($i, 'down') ){

				echo "Rolling back database to version ".$i.PHP_EOL;

				$i--;

			}

		}else{

			$i = $current_version;

		}

		if($version === false){

			$version = $i - 1;

		}

		$this->db->where('name', 'database_version');

		$this->db->update('meta', array('value'=>$version));

		

		echo "database migration complete.  Now at version ".$version;

	}

	

	private function database_migrations($version, $direction = 'up'){

		switch($version){

			case 1:

				if($direction == 'up'){

					$user_fields = array(

						'email' => array(

							'type' => 'VARCHAR',

							'constraint' => 255

						),

						'password' => array(

							'type' => 'VARCHAR',

							'constraint' => 255

						),

						'user_type_id' => array(

							'type' => 'INT',

							'constraint' => 10

						),

						'password_reset_token' =>array(

							'type' => 'VARCHAR',

							'constraint' => 255

						),

						'password_reset_timestamp' =>array(

							'type' => 'INT',

							'constraint' => 11

						),
						
						'first_name' => array(
							'type' => 'VARCHAR',
							'constraint' => 255
						),
						'last_name' => array(
							'type' => 'VARCHAR',
							'constraint' => 255
						)

					);

					$this->databasemigrations->create_table('users', $user_fields);

					$user_type_fields = array(

						'name' => array(

							'type' => 'VARCHAR',

							'constraint' => 100

						)

					);

					$this->databasemigrations->create_table('user_types', $user_type_fields);

					

					//Add Data

					$this->db->insert('user_types', array(

						'name' => 'User',

					));

					$this->db->insert('user_types', array(

						'name' => 'Admin',

					));

					$this->db->insert('user_types', array(

						'name' => 'Guest',

					));

					

					$this->load->model('User_model', 'user');

					$user = array(

						'email' => 'admin@admin.com',

						'password' => 'admin',

						'user_type_id' => 2,
						'first_name' => 'Fake',
						'last_name' => 'Account'

					);

					$this->user->add($user, false);

					if(!$this->user->isValid){

						print_r($this->user->validation_errors);

					}

					

				}else{

					$this->databasemigrations->drop_table('users');

					$this->databasemigrations->drop_table('user_types');

				}

				return true;

			break;

			default:

				return false;

			break;

		}

	}

	







}



?>